package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.UserMapper;
import com.hqyj.pojo.DataMessage;
import com.hqyj.pojo.Role;
import com.hqyj.pojo.User;
import com.hqyj.service.UserService;
import com.hqyj.util.MdFiveUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service

public class UserServiceImpl implements UserService {
    @Resource
    UserMapper userMapper;
    @Autowired
    MdFiveUtil mdFiveUtil;
    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest request;

    @Override
    public DataMessage login(User user) {
        DataMessage data = new DataMessage();
        try {
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(),user.getUserPwd());
            subject.login(token);
            String inputStr = request.getParameter("code");
            String random = (String)session.getAttribute("random");
            if (inputStr.equals(random)){
            data.setInfo("登陆成功");
            data.setSuccess(true);
            }else {
            data.setInfo("验证码错误");
            data.setSuccess(false);
            }

        }catch (
            UnknownAccountException e){
            System.out.println("用户名不存在");
            data.setSuccess(false);
            data.setInfo("用户名不存在");
    }catch (
    IncorrectCredentialsException e){
        System.out.println("密码错误");
        data.setSuccess(false);
        data.setInfo("密码错误");
    }
        return data;
    }

    @Override
    public DataMessage queryAll(User user) {
        DataMessage data = new DataMessage();
        //定义分页参数
        PageHelper.startPage(user.getPage(),user.getRow());
        //查询所有
        List<User> list = userMapper.queryAll(user);
        //创建分页结果集
        PageInfo<User> pageResult = new PageInfo<>(list);
        //判断分页结果是否有值
        if(pageResult != null){
            Map<String,Object> map = new HashMap<>();
            //当前页集合
            map.put("list",pageResult.getList());
            //总页数
            map.put("totalPage",pageResult.getPages());
            //总条数
            map.put("total",pageResult.getTotal());
            System.out.println("map"+map);
            data.setSuccess(true);
            data.setData(map);
        }else {
            data.setSuccess(false);
            data.setInfo("查询失败");
        }

        return data;
    }

    @Override
    public DataMessage updateByUserId(User user) {
        DataMessage data = new DataMessage();
        userMapper.updateByPrimaryKey(user);
        String pwd = null;
        if(user.getSalt()==null){
            String salt = UUID.randomUUID()+"";
            pwd = mdFiveUtil.changePwd(user.getUserPwd(),salt);
        }else {
            pwd = mdFiveUtil.changePwd(user.getUserPwd(),user.getSalt());
        }
        user.setUserPwd(pwd);
        int num =userMapper.updateByPrimaryKey(user);
        if (num > 0){
            data.setSuccess(true);
            data.setInfo("更改成功");
        }else {
            data.setSuccess(false);
            data.setInfo("更改失败");
        }
        return data;
    }

    @Override
    public DataMessage updateDeleteByUserId(User user) {
        DataMessage data = new DataMessage();
        int num = userMapper.updateDeletedByUserId(user);
        if (num > 0){
            data.setSuccess(true);
            data.setInfo("更改成功");
        }else {
            data.setSuccess(false);
            data.setInfo("更改失败");
        }
        return data;
    }

    @Override
    public DataMessage add(User user) {
        DataMessage data = new DataMessage();
        user.setDeleted(0);
        String salt =UUID.randomUUID().toString();
        user.setSalt(salt);
        String newPwd =mdFiveUtil.changePwd(user.getUserPwd(),salt);
        user.setUserPwd(newPwd);
        int num = userMapper.insert(user);
        if (num > 0){
            data.setSuccess(true);
            data.setInfo("添加员工成功");
        }else {
            data.setSuccess(false);
            data.setInfo("添加员工失败");
        }
        return data;
    }



    @Override
    public DataMessage queryByName(User user) {
        DataMessage data = new DataMessage();
        //定义分页参数
        PageHelper.startPage(user.getPage(), user.getRow());
        //查询所有
        List<User> list = userMapper.queryByName(user.getName());
        //创建分页结果集
        PageInfo<User> pageResult = new PageInfo<>(list);
        //判断分页结果是否有值
        if (pageResult != null) {
            Map<String, Object> map = new HashMap<>();
            //当前页集合
            map.put("list", pageResult.getList());
            //总页数
            map.put("totalPage", pageResult.getPages());
            //总条数
            map.put("total", pageResult.getTotal());
            System.out.println("map" + map);
            data.setSuccess(true);
            data.setData(map);
        } else {
            data.setSuccess(false);
            data.setInfo("查询失败");
        }
        return data;
    }

    @Override
    public DataMessage queryAllTwo(User user) {
        DataMessage data = new DataMessage();
        //定义分页参数
        PageHelper.startPage(user.getPage(),user.getRow());
        //查询所有
        List<User> list = userMapper.queryAllTwo(user);
        //创建分页结果集
        PageInfo<User> pageResult = new PageInfo<>(list);
        //判断分页结果是否有值
        if(pageResult != null){
            Map<String,Object> map = new HashMap<>();
            //当前页集合
            map.put("list",pageResult.getList());
            //总页数
            map.put("totalPage",pageResult.getPages());
            //总条数
            map.put("total",pageResult.getTotal());
            System.out.println("map"+map);
            data.setSuccess(true);
            data.setData(map);
        }else {
            data.setSuccess(false);
            data.setInfo("查询失败");
        }

        return data;
    }

    @Override
    public DataMessage allUserRole(User user) {
        DataMessage data = new DataMessage();
        List<User> list = userMapper.queryAllUserRole(user);
        if(list.size()>0){
            data.setSuccess(true);
            data.setData(list);
        }else{
            data.setSuccess(false);
            data.setInfo("查询失败");
        }
        return data;
    }

    @Override
    public DataMessage queryAllNoRole(User user) {
        DataMessage data = new DataMessage();
        List<User> list = userMapper.queryAllNoRole(user);
        if(list.size()>0){
            data.setSuccess(true);
            data.setData(list);
        }else{
            data.setSuccess(false);
            data.setInfo("查询失败");
        }
        return data;
    }

    @Override
    public DataMessage queryAllSales(User user) {
        DataMessage data = new DataMessage();
        List<User> list = userMapper.queryAllSales(user);
        System.out.println("------list----"+list);
        if(list.size()>0){
            data.setSuccess(true);
            data.setData(list);
        }else{
            data.setSuccess(false);
            data.setInfo("查询失败");
        }
    return data;
    }

    @Override
    public DataMessage name(User user) {
        DataMessage data =new DataMessage();
        List<User> list =userMapper.queryName(user);
        if(list.size()>0){
            data.setSuccess(true);
            data.setData(list);
        }else {
            data.setSuccess(false);
            data.setInfo("查询失败");
        }
        return data;
    }

}
